package com.ygk.bluepath.core.View;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.ygk.bluepath.R;
import com.ygk.bluepath.core.BActivity;
import com.ygk.bluepath.bean.LocationBean;
import com.ygk.bluepath.core.Presenter.WatchingPresenter;

import utils.GpsUtil;

public class WatchingActivity extends BActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener, WatchingView {

    @ViewInject(R.id.tv_in)
    private TextView tv_in;
    @ViewInject(R.id.tv_out)
    private TextView tv_out;
    @ViewInject(R.id.tv_through)
    private TextView tv_through;
    @ViewInject(R.id.tv_lng_and_lat)
    private TextView tv_lng_and_lat;


    @ViewInject(R.id.bt_appoint)
    private Button bt_appoint;
    @ViewInject(R.id.bt_reset)
    private Button bt_reset;
    @ViewInject(R.id.bt_record)
    private Button bt_record;

    @ViewInject(R.id.sw_in)
    private Switch sw_in;
    @ViewInject(R.id.sw_out)
    private Switch sw_out;

    private WatchingPresenter wacthingPresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wacthing);
        ViewUtils.inject(this);
        init();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 判断GPS是否正常启动
        if (!GpsUtil.isGpsOpen(this)) {
            tt("请开启GPS导航..."); // 返回开启GPS导航设置界面
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivityForResult(intent, 0);
            return;
        }
    }

    private void init() {
        wacthingPresenter = new WatchingPresenter(this, this);
        wacthingPresenter.setViewTiTle("模拟行驶");


        sw_in.setTag("in");
        sw_in.setOnCheckedChangeListener(this);
        sw_out.setTag("out");
        sw_out.setOnCheckedChangeListener(this);
    }

    @OnClick(value = {R.id.tv_back, R.id.bt_appoint, R.id.bt_reset, R.id.bt_record})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.bt_appoint://一键预约
                //重置状态，将数据表的内容读出缓存
                wacthingPresenter.appoint();
                v.setEnabled(false);
                break;
            case R.id.bt_reset:

                tv_in.setText("");
                tv_out.setText("");
                tv_through.setText("");
                tv_lng_and_lat.setText("初始化完成");
                sw_in.setClickable(true);
                sw_in.setChecked(false);
                sw_out.setClickable(true);
                sw_out.setChecked(false);
                bt_appoint.setEnabled(true);
                wacthingPresenter.reset();
                break;
            case R.id.bt_record:
                sta(this, RecorderActivity.class);
                break;
        }
    }


    @Override
    public void onStart() {
        super.onStart();
        wacthingPresenter.reGisterReceiver(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        wacthingPresenter.unregisterReceiver();
    }


    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        wacthingPresenter.stopService();
        super.onDestroy();
    }

    private LocationBean lbBean = new LocationBean();

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (buttonView.getTag() != null) {
            String tagString = buttonView.getTag().toString();
            if (tagString.equals("in")) {
                if (isChecked) {
                    wacthingPresenter.driveIn();
                    buttonView.setClickable(false);
                }
            } else if (tagString.equals("out")) {
                if (isChecked) {
                    wacthingPresenter.driveOut();
                    buttonView.setClickable(false);
                }
            }

        }
    }

    @Override
    public void lbsResult(double longitude, double latitude) {
        lbBean.setLatitude(latitude);
        lbBean.setLongitude(longitude);
    }

    public void setTips(String tips) {
        tv_lng_and_lat.setText(tips);
    }

    @Override
    public void setTextThrough(String throughString) {
        tv_through.setText(throughString);
    }

    @Override
    public void setTextIn(String s) {
        tv_in.setText(s);
    }

    @Override
    public void setTextOut(String s) {
        tv_out.setText(s);
    }


}
